import {app,router,store} from './app'




export default function(context) {
	console.log(context)
  const s = Date.now()
  return new Promise((resolve, reject) => {
    router.push(context.url)
    router.onReady(()=>{
        const matchedComponents = router.getMatchedComponents();
        // 没有匹配到路由，返回一个包含 404 的 Promise 对象
        if (!matchedComponents.length) {
          return Promise.reject({ code: '404' })
        }

        return Promise.all(matchedComponents.map(component => {
          if (component.preFetch) {
            return component.preFetch(store)
          }
        })).then(() => {
          console.log(`data pre-fetch: ${Date.now() - s}ms`)
          context.initialState = store.state;
          context.state = store.state
          //return app
          resolve(app)
        }).catch(reject)
    })
  });
};
